package archer.framework.common.service;

import archer.framework.common.protocol.result.WebExecuteResultProcessor;
import archer.framework.common.repository.condition.QueryCondition;
import archer.framework.common.repository.dao.GenericDao;
import archer.framework.common.repository.page.PageInfo;
import archer.framework.utils.ContextUtils;
import com.avaje.ebean.ExpressionList;
import com.avaje.ebean.Query;

import java.util.Collection;

/**
 * @author christ
 * @date 2016/4/26
 */
public class BaseService extends WebExecuteResultProcessor {

    protected static GenericDao dao() {

        return ContextUtils.getBean("genericDao", GenericDao.class);
    }

    protected PageInfo findPagedList(Integer pageIndex, Integer pageSize, Query query) {

        return dao().findPagedList(pageIndex, pageSize, query);
    }

    protected PageInfo findPagedList(Integer pageIndex, Integer pageSize, ExpressionList expressionList) {

        return dao().findPagedList(pageIndex, pageSize, expressionList);
    }

    protected <T> PageInfo findPagedList(Query<T> query, QueryCondition condition) {

        return dao().findPagedList(query, condition);
    }

    protected <T> void logicalDelete(Collection<T> beans) {

        dao().logicalDelete(beans);
    }

    protected <T> void logicalDelete(Class<T> clazz, Object id) {

        dao().logicalDelete(clazz, id);
    }

    protected <T> void logicalDelete(Class<T> clazz, Collection<Object> ids) {

        dao().logicalDelete(clazz, ids);
    }

    protected <T> void delete(Collection<T> beans) {

        dao().delete(beans);
    }

    protected <T> void delete(Class<T> clazz, Object id) {

        dao().delete(clazz, id);
    }

    protected <T> void delete(Class<T> clazz, Collection<Object> ids) {

        dao().delete(clazz, ids);
    }

    protected GenericDao.SqlUpdater sqlDelete(String table) {

        return dao().sqlDelete(table);
    }

    protected GenericDao.SqlUpdater sqlLogicalDelete(String table) {

        return dao().sqlLogicalDelete(table);
    }

    protected <T> void store(Collection<T> beans) {

        dao().store(beans);
    }

    protected <T> Collection<T> storeAndQuery(Collection<T> beans) {

        return dao().storeAndQuery(beans);
    }
}
